package cn.waka.sys.admin.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import cn.waka.sys.admin.object.Dic;
import cn.waka.sys.admin.object.Result;
import cn.waka.sys.admin.service.DicService;
import cn.waka.sys.admin.utils.Validator;

@RestController
@RequestMapping("/dic")
public class DicController extends BaseController {

    @Autowired
    private DicService dicService;

    @RequestMapping(path = "/type_index", name = "影片分类列表", produces = "text/html; charset=UTF-8")
    public ModelAndView movietypes(HttpServletRequest request, HttpServletResponse response) {
        List<Map<String, Object>> movie_types = dicService.getTypeList("173");
        ModelAndView mv = new ModelAndView();
        mv.addObject("pid", 173);
        mv.addObject("diclist", movie_types);
        return mv;
    }

    @RequestMapping(path = "/index", name = "字典列表", produces = "text/html; charset=UTF-8")
    public ModelAndView dicList(@RequestParam(name = "pid", required = false) int pid, HttpServletRequest req, HttpServletResponse res) {
        List<Dic> dicList = dicService.getDicByPid(pid);
        ModelAndView mv = new ModelAndView();
        mv.addObject("pid", pid);
        mv.addObject("diclist", dicList);
        return mv;
    }

    @RequestMapping(path = "/edit", name = "查一个字典", produces = "text/html; charset=UTF-8")
    public ModelAndView edit(@RequestParam(name = "id", required = false) String idStr, @RequestParam(name = "pid", required = false) int pid, HttpServletRequest req, HttpServletResponse res) {
        int id = -99;
        if (idStr != null && Validator.isNum(idStr)) {
            id = Integer.parseInt(idStr);
        }
        Dic dic = dicService.getDicById(id);
        if (dic == null) {
            dic = new Dic();
        }

        List<Dic> dicList = dicService.getDicMenu(pid, id);
        ModelAndView mv = new ModelAndView();
        mv.addObject("obj", dic);
        mv.addObject("diclist", dicList);
        return mv;
    }

    @RequestMapping(path = "/save", name = "保存字典", produces = "application/json; charset=UTF-8")
    public Result save(HttpServletRequest req, HttpServletResponse res) {
        String idStr = req.getParameter("id");
        int id = 0;
        if (idStr != null && Validator.isNum(idStr)) {
            id = Integer.parseInt(idStr);
        }
        String name = req.getParameter("name");
        String pidStr = req.getParameter("pid");
        int pid = 0;
        if (pidStr != null && Validator.isNum(pidStr)) {
            pid = Integer.parseInt(pidStr);
        }
        String icon = req.getParameter("icon");

        Dic d = new Dic();
        d.setId(id);
        d.setName(name);
        d.setIcon(icon);
        d.setPid(pid);
        int rs = dicService.saveDic(d);

        if (rs > 0) {
            return error(1, "保存成功！");
        } else {
            return error(0, "保存失败！");
        }
    }

    @RequestMapping(path = "/updateStatus", name = "字典启用|禁用", produces = "application/json; charset=UTF-8")
    public Result updateStatus(HttpServletRequest req, HttpServletResponse res) {
        String idStr = req.getParameter("id");
        int id = 0;
        if (idStr != null && Validator.isNum(idStr)) {
            id = Integer.parseInt(idStr);
        }
        String statusStr = req.getParameter("status");
        int status = 0;
        if (statusStr != null && Validator.isNum(statusStr)) {
            status = Integer.parseInt(statusStr);
        }
        int rs = dicService.isDisable(id, status);
        if (rs > 0) {
            return error(1, "操作成功！");
        } else {
            return error(0, "操作失败！");
        }
    }

    @RequestMapping(path = "/del", name = "删除字典", produces = "application/json; charset=UTF-8")
    public Result del(HttpServletRequest req, HttpServletResponse res) {
        String idStr = req.getParameter("id");
        int id = 0;
        if (idStr != null && Validator.isNum(idStr)) {
            id = Integer.parseInt(idStr);
        }
        int rs = dicService.deleteDic(id);
        if (rs > 0) {
            return error(1, "操作成功！");
        } else {
            return error(0, "操作失败！");
        }
    }
}
